package com.education.common.config;

import com.education.common.cache.CacheBean;
import com.education.common.cache.FstRedisSerializer;
import com.education.common.cache.RedisCacheBean;
import com.education.common.model.JwtToken;
import com.education.common.model.OnlineUserManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author 余一
 * @Description
 * @date 2021/7/10 22:12
 */
@Configuration
public class BeanConfig {
    @Bean
    public JwtToken jwtToken(){
        return new JwtToken("education-admin");
    }

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        //配置redisTemplate
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置序列化
        FstRedisSerializer fstRedisSerializer = new FstRedisSerializer();
        redisTemplate.setKeySerializer(fstRedisSerializer);//key序列化
        redisTemplate.setValueSerializer(fstRedisSerializer);
        redisTemplate.setHashKeySerializer(fstRedisSerializer);
        redisTemplate.setHashValueSerializer(fstRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    public CacheBean redisCacheBean(RedisTemplate redisTemplate){
        return new RedisCacheBean(redisTemplate);
    }

    @Bean
    public OnlineUserManager onlineUserManager(CacheBean redisCacheBean){
        return new OnlineUserManager(redisCacheBean);
    }

}
